<?php

namespace app\admin\model\hd\forum;

use think\Model;
use app\admin\model\hd\forum\Likes;


class Info extends Model
{





    // 表名
    protected $name = 'hd_forum_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_pinned_text',
        'is_locked_text',
        'create_time_text',
        'update_time_text',
        'zan_num'
    ];



    public function getIsPinnedList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }

    public function getIsLockedList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }


    public function getIsPinnedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pinned']) ? $data['is_pinned'] : '');
        $list = $this->getIsPinnedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsLockedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_locked']) ? $data['is_locked'] : '');
        $list = $this->getIsLockedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function type()
    {
        return $this->belongsTo('app\admin\model\hd\forum\Type', 'type_id', 'id')->setEagerlyType(0);
    }
    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id')->setEagerlyType(0);
    }
    public function getZanNumAttr()
    {
        // $ids = explode(',', $this->department_id);
        return Likes::whereIn('in_id', $this->id)->count();
    }
}
